package com.niodata.dp.monitor.sharelib.service;

import com.niodata.dp.monitor.sharelib.entity.base.BaseResult;
import com.niodata.dp.monitor.sharelib.view.file.FileMonitorInfoView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class FileMonitorService {

  @Autowired
  private RestTemplate restTemplate;

  @Value("${monitor.server.address}")
  private String server;
  @Value("${monitor.server.port}")
  private String port;

  public BaseResult startFileMonitorSchedule(FileMonitorInfoView view) {
    String execUrl = String.format("http://%s:%s/monitor/file/monitor/start", server, port);
    ResponseEntity<BaseResult> responseEntity = restTemplate
          .postForEntity(execUrl, view, BaseResult.class);
    return responseEntity.getBody();
  }

  public BaseResult stopFileMonitorSchedule(FileMonitorInfoView view) {
    String execUrl = String.format("http://%s:%s/monitor/file/monitor/stop", server, port);
    ResponseEntity<BaseResult> responseEntity = restTemplate
          .postForEntity(execUrl, view, BaseResult.class);
    return responseEntity.getBody();
  }

  public BaseResult deleteFileMonitorSchedule(FileMonitorInfoView view) {
    String execUrl = String
          .format("http://%s:%s/monitor/file/monitor/del", server, port);
    ResponseEntity<BaseResult> responseEntity = restTemplate
          .postForEntity(execUrl, view, BaseResult.class);
    return responseEntity.getBody();
  }
}
